/*
 * @(#)ShopMqLogServiceImpl.java 2011-11-21
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.shangjia.common.exceptions.Logger;
import cn.shangjia.common.mq.exec.message.DefaultKnetMessage;
import cn.shangjia.common.mq.exec.message.KnetMessage;
import cn.shangjia.common.mq.exec.message.KnetMessageHead;
import cn.shangjia.common.util.xml.BeanSerialization;
import cn.shangjia.shop.dao.common.MqFailureLogDao;
import cn.shangjia.shop.log.ShopLoggerFactory;
import cn.shangjia.shop.service.MqLogService;

/**
 * MQ异常日志入库实现类.
 *
 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
 * @version 1.0, 2011-11-21 13:34:50
 * @since 1.0
 */
@Service
public class MqLogServiceImpl implements MqLogService {
	
	private static final Logger logger = ShopLoggerFactory.getLogger(MqLogServiceImpl.class);
	
	@Resource
	private MqFailureLogDao mqFailureLogDao;

	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.MqLogService#addLog(cn.shangjia.common.mq.exec.message.KnetMessage, java.lang.String)
	 */
	@Override
	public void addLog(KnetMessage message, String errorMessage) {
		
		try {
		
			if (message == null) {
				logger.warn("消息错误日志message对象为空!");
				return ;
			}
			
			Map<KnetMessageHead, Object> headers = message.getHeads();
			
			if (headers == null) {
				logger.warn("消息错误日志head对象为空!");
				return;
			}
			
			logger.error("同步有异常发生,消息ID为:", headers.get(KnetMessageHead.TransactionID), "请及时核对数据");
			
			Map<String, Object> obj = new HashMap<String, Object>();
			
			for (KnetMessageHead head : headers.keySet()) {
				obj.put(head.getAlias(), String.valueOf(headers.get(head)));
			}
			
			Map<String, Object> bodys = message.getBodys();
			
			if (bodys == null) {
				logger.warn("消息错误日志body对象为空! messageHeaders = " , headers);
				return;
			}
			
			obj.put("msgBody", BeanSerialization.beanToXml(new HashMap<String, Object>(bodys)));
			obj.put("state", "0");
			obj.put("failureReason", errorMessage);
			obj.put("failureTime", new Date());
			
			mqFailureLogDao.create(obj);
			
		} catch(Exception e) {
			logger.error(e, "消息错误日志有异常发生!!!!!!!", "message=", message, "errorString=", errorMessage);
		}
		
	}

	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.MqLogService#getMessage(java.lang.String)
	 */
	@Override
	public KnetMessage getMessage(String messageId) {
		
		Map<String, String> map = mqFailureLogDao.read(messageId);
		KnetMessage message = DefaultKnetMessage.newInstance();
		if (map == null)
			return message;
		
		for (KnetMessageHead head : KnetMessageHead.values()) {
			String key = head.toString().toUpperCase();
			String value = map.get(key);
			if (value == null) {
				continue;
			}
			message.addHead(head, value);
		}
		
		String xml = map.get("MSGBODY");
		Map<String, Object> bodys = BeanSerialization.xmlToBean(xml, new HashMap<String, Object>());
		message.setBodys(bodys);
		
		return message;
	}

}
